// pages/other/other.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    handlingFeeFlag:false,//手续费弹框
    tipsFlag: false,
    imgUrl: "https://head.image.jiajijie.com/dj/",
    beginValue: "",
    numIndex: 0,
    index: 0,
    numArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    cityFlag: false, //城市
    city: "",
    priceFlag: false,
    price: "",
    beginFlag: false,
    numFlag: false,
    all: [],
    dialogFlag: true,
    pictureUrl:"/image/"
  },
  refreshPay() {
    this.requestApi()
  },
  // 取消支付
  closePay() {
    wx.redirectTo({
      url: `../orderMsg/orderMsg?order_id=${this.data.orderid}&fromTo=true`
    })
  },
  dialog(){
    this.setData({
      handlingFeeFlag: true
    })
  },
  // 
  tipConfirm(e) {
    if (e.detail) {
      this.request()
    }
  },
  closeBtn() {
    this.setData({
      dialogFlag: false
    })
  },
  warn() {
    wx.showModal({
      content: '通道费为微信收取，不可退还！',
      showCancel: false
    })
  },
  // 缴纳开始时间
  bindTimeChange(e) {
    this.setData({
      beginValue: e.detail.value
    })
    this.time();

  },
  // 服务时间
  bindNumChange(e) {
    this.setData({
      numIndex: e.detail.value
    })
    this.time();
  },
  // 输入城市
  cityInput(e) {
    this.setData({
      city: e.detail.value.replace(/\s/g, "")
    })
  },
  priceInput(e) {
    this.setData({
      price: e.detail.value.replace(/[^\d^\.]+/g, '')
    })

  },
  // 支付
  payFor() {

    let flag = true
    if (this.data.city == "") {
      flag = false
      this.setData({
        cityFlag: true
      })
      return
    } else {
      flag = true
      this.setData({
        cityFlag: false
      })
    }

    if (this.sort(this.data.price) == false || this.data.price == "") {
      flag = false
      this.setData({
        priceFlag: true
      })
      return
    } else {
      flag = true
      this.setData({
        priceFlag: false
      })
    }
    // let agreeShow = wx.getStorageSync('agreeShow')
    // if (!agreeShow) {
    //   this.setData({
    //     tipsFlag: true
    //   })
    //   return
    // }

    if (flag) {
      this.requestApi()
    } 
  },

  requestApi(){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this
    let val = {
      city_name: this.data.city,
      amount: this.data.price,
      ipid: this.data.query.ipid,
      start_date: this.data.beginValue,
      count: this.data.all.length,
      is_new: this.data.query.is_new,
    }
    app.request('daijiao/order/other_save', val, true).then(res => {
      wx.hideLoading()
      if (res.error_code == 0) {
        this.setData({
          orderid: res.data.order_id
        })
        app.request('daijiao/order/pay', {
          order_id: res.data.order_id
        }, true).then(ress => {
          if (res.error_code == 0) {
            let order = res.data.order_id
            const obj = {
              timeStamp: ress.data.timeStamp,
              nonceStr: ress.data.nonceStr,
              package: ress.data.package,
              signType: 'MD5',
              paySign: ress.data.paySign,
            };
            wx.requestPayment({
              ...obj,
              success(res) {
                wx.redirectTo({
                  // url: `../orderMsg/orderMsg?order_id=${order}&fromTo=true`
                  url: `../payment/payment?order_id=${order}&fromTo=true`

                })
              },
              fail(res) {
                that.setData({
                  payFlag: true
                })
              }
            })
          }else{
            wx.showModal({
              content: res.error_message,
              showCancel:false
            })
          }
        })
      }
    })
  },
  
  sort(word) {
    let num = word.toString();
    let arr = num.split('.');
    if (arr.length > 2) {
      return false
    } else {
      return true
    }
  },

  time() {
    if (this.data.beginValue !== "" && this.data.numIndex >= 0) {
      let timeDate = this.data.beginValue;
      let date = new Date(timeDate)
      let year = date.getFullYear()
      let month = date.getMonth() + 1;
      let arr = []
      arr.push(year + "年" + month + "月")
      for (let i = 1; i < this.data.numArray[this.data.numIndex]; i++) {
        month += 1
        if (month > 12) {
          arr.push(parseInt(year) + 1 + "年" + this.formatNumber(parseInt(month - 12)) + '月')
        } else {
          arr.push(year + "年" + this.formatNumber(month) + '月')
        }
      }
      this.setData({
        all: arr
      })
    }
  },
  // 月份处理
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let timer = new Date();
    let year = timer.getFullYear();
    let month = timer.getMonth() + 1;
    let beginValue = year + '-' + this.formatNumber(month)
    this.setData({
      beginValue
    })
    wx.nextTick(() => {
      this.time()
    })
    if (options.query) {
      let query = JSON.parse(options.query);
      this.setData({
        query: query
      })
    }
    wx.getUserInfo({
      success: res => {
        this.setData({
          userInfo: res.userInfo
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "社保/公积金代缴、公积金提取,还可领失业金哦!",
      path: '/pages/index/index',
      imageUrl: 'https://head.image.jiajijie.com/dj/images/fiveshareBg.png',
      success: function (res) {},
      fail: function (res) {
        console.log(res)
      }
    }
  }
})